<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/7/4
 * Time: 11:46
 */

namespace app\admin\controller\system;


use app\common\controller\AdminController;
use think\app;
use think\facade\Db;

class Configuration extends AdminController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = app('app\common\model\system\Configuration');

    }


    public function index()
    {
        $groupsmodel = app('app\common\model\system\ConfigGroup');
        $groups = $groupsmodel->with(['configurations'=>function($query){
            $query->where('status',1);
        }])->order('sort asc')->select();

        return View('',['groups'=>$groups]);
    }

    /**
     * 更新配置
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function edit()
    {
        $data = $this->request->param();

        Db::startTrans();
        try{
            foreach($data as $k=>$v){
                Db::table('bgt_configuration')->where('key',$k)->update(['val'=>$v]);
            }
            Db::commit();
        }catch (\Exception $exception){
            Db::rollback();
            return json(['code'=>1,'msg'=>'更新失败']);
        }

        return json(['code'=>0,'msg'=>'更新成功']);
    }
}